﻿using log4net;
using Microsoft.Extensions.Configuration;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using WLYT.Common;
using WLYT.Domain.Model;

namespace WLYT.Repository
{
    public class DbContext
    {
        private static readonly ILog loginfo = LogManager.GetLogger("loginfo");
        public static SqlSugarScope Db = new SqlSugarScope(new List<ConnectionConfig>()
        {
            new ConnectionConfig()
            {
                ConfigId = "db1",
                DbType = DbType.SqlServer,
                ConnectionString = ConfigExtensions.Configuration.GetConnectionString("wmsConnection"),
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                MoreSettings = new ConnMoreSettings
                {
                    IsWithNoLockQuery = true//开启全局查询 with nolock
                }
            }
        },
        db =>
        {
            db.GetConnectionScope("db1").Aop.OnLogExecuting = (sql, pars) =>
            {
                if (!sql.Contains("DataBaseLog") && !sql.Contains("LogRecord") && !sql.Contains("AgvLog"))
                {
                    DataBaseLog log = new DataBaseLog
                    {
                        IP = StringHelper.GetIP(),
                        Param = Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)),
                        Sql = sql
                    };
                    loginfo.Warn(log);
                }
            };
        });
    }
}
